package com.itheima.eventbusdemo;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import de.greenrobot.event.EventBus;

public class LeftFragment extends ListFragment {

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		String[] strs = new String[]{"主线程消息1", "子线程消息1", "主线程消息2","通过普通类发送消息"};
		setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, strs));
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {

		switch (position) {
			case 0:
				// 主线程
				System.out.println("----------------------主线程发的消息1"
						+ " threadName: "+ Thread.currentThread().getName()
						+ " threadId: " + Thread.currentThread().getId());
				EventBus.getDefault().post(new MsgEvent1("主线程发的消息1"));
				break;
			case 1:
				// 子线程
				new Thread(){
					public void run() {
						System.out.println("----------------------子线程发的消息1"
								+ " threadName: "+ Thread.currentThread().getName()
								+ " threadId: " + Thread.currentThread().getId());
						EventBus.getDefault().post(new MsgEvent1("子线程发的消息1"));
					};
				}.start();

				break;
			case 2:
				// 主线程
				System.out.println("----------------------主线程发的消息2"
						+ " threadName: "+ Thread.currentThread().getName()
						+ " threadId: " + Thread.currentThread().getId());
				EventBus.getDefault().post(new MsgEvent2("主线程发的消息2"));
				break;
			case 3:
				// 子线程2
				new Thread(){
					public void run() {
						System.out.println("----------------------子线程发的类EventBus1消息1"
								+ " threadName: "+ Thread.currentThread().getName()
								+ " threadId: " + Thread.currentThread().getId());
						new EventBus1();
					}
				}.start();
		}
	}

}
